package com.neusoft.ruidaozaixian.controller.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Window;
import android.widget.RadioGroup;

import com.neusoft.ruidaozaixian.R;
import com.neusoft.ruidaozaixian.controller.fragment.LearningPhaseFragment;
import com.neusoft.ruidaozaixian.controller.fragment.PostInformationFragment;

import java.util.ArrayList;

public class PostActivity extends FragmentActivity implements RadioGroup.OnCheckedChangeListener{

    //ViewPager控件
    private ViewPager viewPager ;
    //RadioGroup控件
    private RadioGroup radioGroup ;
    //类型为Fragment的动态数组
    private ArrayList<Fragment> fList= new ArrayList<Fragment>() ;

    private long startTime=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.activity_post);
        //注册radiobutton选择监听器
        radioGroup = (RadioGroup) findViewById(R
                .id.radioGroup) ;
        radioGroup.setOnCheckedChangeListener(this);
        //ViewPager初始化函数
        initViewPager();



    }

    private void initViewPager()	{
        viewPager = (ViewPager) findViewById(R.id.viewPager);

        Fragment pf = new PostInformationFragment() ;

        Fragment lf = new LearningPhaseFragment();

        //将各Fragment加入数组中
        fList.add(pf);
        fList.add(lf);
        //设置ViewPager的适配器
        viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
        //当前为第一个页面
        int id =getIntent().getIntExtra("id", 0);
        if (id==1) {
            viewPager.setCurrentItem(2);
            radioGroup.check(R.id.radioButton_learning);
        }else {
            viewPager.setCurrentItem(0);
        }
        //ViewPager的页面改变监听器，可先不加这个监听器，此监听器用于滑动切换Fragment时改变RadioButton的选中状态
        viewPager.setOnPageChangeListener((ViewPager.OnPageChangeListener) new MyListener());
    }


    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        // TODO Auto-generated method stub
        //获取当前被选中的RadioButton的ID，用于改变ViewPager的当前页
        int current=0;
        switch(checkedId)
        {
            case R.id.radioButton_post:
                current = 0 ;
                break ;
            case R.id.radioButton_learning:
                current = 1 ;
                break;
        }
        if(viewPager.getCurrentItem() != current){
            viewPager.setCurrentItem(current);
        }

    }


    public class MyAdapter extends FragmentPagerAdapter {

        public MyAdapter(FragmentManager fm) {
            super(fm);
            // TODO Auto-generated constructor stub
        }
        @Override
        public Fragment getItem(int position) {
            return fList.get(position);
        }
        @Override
        public int getCount() {
            return fList.size();
        }
    }

    public class MyListener implements ViewPager.OnPageChangeListener {
        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }
        @Override
        public void onPageSelected(int arg0) {
            //获取当前页面用于改变对应RadioButton的状态
            int current = viewPager.getCurrentItem() ;
            switch(current)
            {
                case 0:
                    radioGroup.check(R.id.radioButton_post);//将radiobutton设为选中
                    break;
                case 1:
                    radioGroup.check(R.id.radioButton_learning);
                    break;

            }
        }
    }
}
